package com.sino.business.service;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

import com.sino.bean.Hjqk;
import com.sino.bean.Zljszk;
import com.sino.business.IZljszkService;
import com.sino.dao.IBasicInfoDAO;
import com.sino.model.ZljszkModel;

public class ZljszkServiceImp implements IZljszkService {
	private IBasicInfoDAO basicInfoDAO = null;
	/**
     * Description       	   ：保存专利技术状况信息到数据库中
     * @param zljjzk 	   	   ：专利技术状况信息持久化對象
     */
	public void saveZljszk(ZljszkModel zljszkModel){
		Zljszk zljszk = this.getBasicInfoDAO().findZljszkByZlIdClear(zljszkModel.getZlid());
		if(zljszk == null){
			this.getBasicInfoDAO().saveZljszk(this.getZljszk(zljszkModel));
		}else{
			this.getBasicInfoDAO().updateZljszk(this.getZljszk(zljszkModel));
		}
	}
	/**
     * Description       	   ：更新专利技术状况信息到数据库中
     * @param zljjzk 	   	   ：专利技术状况信息持久化對象
     */
	public void updateZljszk(ZljszkModel zljszkModel){}
	/**
     * Description       	   ：根据专利编号查找专利技术状况信息
     * @param zlId		 	   ：专利编号
     */
	public ZljszkModel findZljszkByZlId(String zlId){
		return this.getZljszkModel(this.getBasicInfoDAO().findZljszkByZlId(zlId));
	}
	
	//-------------------------------------------数据转换
	public ZljszkModel getZljszkModel(Zljszk zljszk){
		ZljszkModel zljszkModel = new ZljszkModel();
		if(zljszk == null){
			return zljszkModel;
		}
		Set hjqkSet =  zljszk.getHjqks();
		Hjqk hjqk;
		Iterator it = hjqkSet.iterator();
		int i = 1;
		while(it.hasNext()){
			hjqk = (Hjqk)it.next();
			try{
				BeanUtils.copyProperty(zljszkModel, "id"+i, hjqk.getId());
				BeanUtils.copyProperty(zljszkModel, "zljszk"+i, hjqk.getZljszk());
				BeanUtils.copyProperty(zljszkModel, "nd"+i, hjqk.getNd());
				BeanUtils.copyProperty(zljszkModel, "bjbm"+i, hjqk.getBjbm());
				BeanUtils.copyProperty(zljszkModel, "hjmc"+i, hjqk.getHjmc());
				BeanUtils.copyProperty(zljszkModel, "hjdj"+i, hjqk.getHjdj());
				i++;
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		zljszkModel.setZlid(zljszk.getZlid());
		zljszkModel.setZjjsinfo(zljszk.getZjjsinfo());
		return zljszkModel;
	}
	public Zljszk getZljszk(ZljszkModel zljszkModel){
		Zljszk zljszk =  new Zljszk();
		Hjqk hjqk1 = new Hjqk();
		Hjqk hjqk2 = new Hjqk();
		Hjqk hjqk3 = new Hjqk();
		Hjqk hjqk4 = new Hjqk();
		//Hjqk hjqk5 = new Hjqk();
		//Hjqk hjqk6 = new Hjqk();
		
		zljszk.setZjjsinfo(zljszkModel.getZjjsinfo());
		zljszk.setZlid(zljszkModel.getZlid());
		
		hjqk1.setBjbm(zljszkModel.getBjbm1());
		hjqk1.setHjdj(zljszkModel.getHjdj1());
		hjqk1.setHjmc(zljszkModel.getHjmc1());
		hjqk1.setId(zljszkModel.getId1());
		hjqk1.setNd(zljszkModel.getNd1());
		
		hjqk2.setBjbm(zljszkModel.getBjbm2());
		hjqk2.setHjdj(zljszkModel.getHjdj2());
		hjqk2.setHjmc(zljszkModel.getHjmc2());
		hjqk2.setId(zljszkModel.getId2());
		hjqk2.setNd(zljszkModel.getNd2());
		
		hjqk1.setZljszk(zljszk);
		hjqk2.setZljszk(zljszk);
		hjqk3.setZljszk(zljszk);
		hjqk4.setZljszk(zljszk);
		//hjqk5.setZljszk(zljszk);
		//hjqk6.setZljszk(zljszk);
		
		hjqk3.setBjbm(zljszkModel.getBjbm3());
		hjqk3.setHjdj(zljszkModel.getHjdj3());
		hjqk3.setHjmc(zljszkModel.getHjmc3());
		hjqk3.setId(zljszkModel.getId3());
		hjqk3.setNd(zljszkModel.getNd3());
		
		hjqk4.setBjbm(zljszkModel.getBjbm4());
		hjqk4.setHjdj(zljszkModel.getHjdj4());
		hjqk4.setHjmc(zljszkModel.getHjmc4());
		hjqk4.setId(zljszkModel.getId4());
		hjqk4.setNd(zljszkModel.getNd4());
		
		/*hjqk5.setBjbm(zljszkModel.getBjbm5());
		hjqk5.setHjdj(zljszkModel.getHjdj5());
		hjqk5.setHjmc(zljszkModel.getHjmc5());
		hjqk5.setId(zljszkModel.getId5());
		hjqk5.setNd(zljszkModel.getNd5());
		
		hjqk6.setBjbm(zljszkModel.getBjbm6());
		hjqk6.setHjdj(zljszkModel.getHjdj6());
		hjqk6.setHjmc(zljszkModel.getHjmc6());
		hjqk6.setId(zljszkModel.getId6());
		hjqk6.setNd(zljszkModel.getNd6());*/
		
		Set hjqks = new HashSet();
		hjqks.add(hjqk1);
		hjqks.add(hjqk2);
		hjqks.add(hjqk3);
		hjqks.add(hjqk4);
		//hjqks.add(hjqk5);
		//hjqks.add(hjqk6);
		
		zljszk.setHjqks(hjqks);
		
		return zljszk;
	}
	public IBasicInfoDAO getBasicInfoDAO() {
		return basicInfoDAO;
	}
	public void setBasicInfoDAO(IBasicInfoDAO basicInfoDAO) {
		this.basicInfoDAO = basicInfoDAO;
	}
	
}
